import { defineStore } from "pinia";
import {logout, login as userLogin} from "@/api/login/loginAPI";
import { msgError } from "@/utils";
import { IUser } from "@/interface/IUser";
import { ILogin } from "@/interface";

const useUserStore = defineStore('user', {
  state() {
    const aigcUser: IUser = {
      id: "",
      name: "",
      token: "",
      account: "",
    }
    return {
      isLogin: false,
      user: aigcUser
    }
  },
  actions: {
   async login(user: ILogin)  {
    // debugger
        console.log("用户登录")
      if (!this.isLogin) {
        // todo 未登录用户请求数据
       const [error,userData]= await userLogin(user)
       this.user.account = userData?.account
       this.user.token = userData?.token
       if(!error){
        this.isLogin = true
       }else{
        // console.log(error)
        // msgError("登录失败！！")
       }
      

      } 
     
      return this.isLogin
    },
    async logout() {
      Object.assign(this.user,{})
      await logout()
      window.location.reload()
      // todo 用户登出数据请求
      this.isLogin = false
    }
  },
  persist:true

})
export default useUserStore;